package Interface.Relation;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import Controleur.ControleurGestionRelation;
import Metier.Contrainte;
import Metier.Table;

public class InterfaceContrainte {

	private Contrainte contrainte;
	private Boolean select = false;
	private ControleurGestionRelation controleur;
	
	public InterfaceContrainte(Contrainte contrainte) {
		this.contrainte = contrainte;
		controleur = ControleurGestionRelation.getInstance();
	}

	public Point GetPoint1(int i)
	{
		return new Point(this.contrainte.getTable1().getX(),this.contrainte.getTable1().getY()+((contrainte.getIndex1().get(i)+1)*18)+9);
	}

	public Point GetPoint2(int i)
	{
		return new Point(this.contrainte.getTable2().getX(),this.contrainte.getTable2().getY()+((contrainte.getIndex2().get(i)+1)*18)+9);
	}

	public Point GetPointDroite1(int i)
	{
		int taille = (int) (this.contrainte.getTable1().getX()+this.contrainte.getTable1().getWidth());
		return new Point(taille,this.contrainte.getTable1().getY()+((contrainte.getIndex1().get(i)+1)*18)+9);
	}

	public Point GetPointDroite2(int i)
	{
		int taille = (int) (this.contrainte.getTable2().getX()+this.contrainte.getTable2().getWidth());
		return new Point(taille,this.contrainte.getTable2().getY()+((contrainte.getIndex2().get(i)+1)*18)+9);
	}

	public int getFirstIndex1() {
		return this.contrainte.getIndex1().get(0);
	}

	public int getFirstIndex2() {
		return this.contrainte.getIndex2().get(0);
	}

	public void dessinerGras(Graphics g) {

		Graphics2D g2 = (Graphics2D) g;
		g2.setStroke(new BasicStroke(2));
		dessiner(g2);
		g2.setStroke(new BasicStroke(1));
	}

	public void dessinerNormal(Graphics g) {
		g.setColor(Color.black);
		if(!this.select)
		{
			Graphics2D g2 = (Graphics2D) g;
			g2.setStroke(new BasicStroke(1));
			dessiner(g2);
		}else
		{
			g.setColor(Color.red);
			Graphics2D g2 = (Graphics2D) g;
			g2.setStroke(new BasicStroke(2));
			dessiner(g2);
			g2.setStroke(new BasicStroke(1));
		}
		g.setColor(Color.black);
	}
	private void dessiner(Graphics2D g) {
		for (int i = 0; i < this.contrainte.getIndex1().size(); i++) {
			Point p1 = GetPoint1(i);
			Point p2 = GetPoint2(i);

			if (p1.getX() > p2.getX() ) {
				p2 = GetPointDroite2(i);
				g.drawLine(p1.x - 8, p1.y, p2.x + 8, p2.y);
				g.drawLine(p2.x + 8, p2.y, p2.x, p2.y);
				g.drawLine(p1.x - 8, p1.y, p1.x, p1.y);
			} else {
				p1 = GetPointDroite1(i);
				g.drawLine(p2.x - 8, p2.y, p1.x + 8, p1.y);
				g.drawLine(p1.x + 8, p1.y, p1.x, p1.y);
				g.drawLine(p2.x - 8, p2.y, p2.x, p2.y);
			}

		}
	}

	public void removeAll()
	{
		this.contrainte.getIndex1().clear();
		this.contrainte.getIndex2().clear();
	}
	public void ajouter(String attr1,String attr2)
	{
		controleur.contrainteAjouterAttributs(this.contrainte,attr1,attr2);	
	}

	public void ajouter(int ind1,String attr1,int ind2,String attr2) {
		controleur.contrainteAjouterAttributs(this.contrainte,ind1,attr1,ind2,attr2);
	}

	public boolean save()
	{
		boolean resultat = controleur.sauvegarderContrainte(this.contrainte);
		return resultat;
	}

	public boolean getContact(int x3, int y3) {
		for (int i = 0; i < this.contrainte.getIndex1().size(); i++)
		{
			Point p2 = GetPoint1(i);
			Point p1 = GetPoint2(i);

			if (p2.getX() > p1.getX()) {
				p1 = GetPointDroite2(i);
			} else {
				p2 = GetPointDroite1(i);
			}
			int x1 = p1.x;
			int y1 = p1.y;
			int x2 = p2.x;
			int y2 = p2.y;
			double a = Math.sqrt(Math.abs((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
			double b = Math.sqrt(Math.abs((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)));
			double c = Math.sqrt(Math.abs((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3)));
			double p = (a+b+c)/2;

			int h = (int) ((2*Math.sqrt( p*(p-a)*(p-b)*(p-c)))/a);
			if (h<5 )
			{
				if((a+20)>(b+c))
				{
					select = true;
				}
			}
		}
		return select;
	}

	public void setSelectFalse() {
		this.select = false;
	}

	public boolean useTable(Table table) {
		return ((table.getNom().equals(this.contrainte.getNomTable1())) || (table.getNom().equals(this.contrainte.getNomTable2())));
	}

	public void masquer() {
		this.controleur.masquerContrainte(this.contrainte);		
	}

	public void delete() {
	this.controleur.supprimerContrainte(this.contrainte);
	}
}
